package com.example.presentshopping.ui.fragment

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpFragment
import com.example.presentshopping.config.bean.BoardFragmentBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.ui.activity.DetailsActivity
import com.example.presentshopping.ui.activity.SupplierActivity
import com.example.presentshopping.ui.custom.RoundImageView
import com.example.presentshopping.utils.glide.GlideUtil
import com.example.presentshopping.utils.tool.GsonUtils
import com.yanzhenjie.recyclerview.SwipeRecyclerView


class BoardFragment : BaseMvpFragment() {

    private var sendMesPresenter: PublicPresenter? = null
    var boardFragmentBean: BoardFragmentBean? = null
    var refresh_layout: SwipeRefreshLayout? = null
    var recycler_view: SwipeRecyclerView? = null

    var baseRecyclerAdapter: BaseRecyclerAdapter<BoardFragmentBean.DataBean.ListBean>? = null
    var goosAdapter: BaseRecyclerAdapter<BoardFragmentBean.DataBean.ListBean.GoodsBean>? = null

    var page = 1
    override val layoutId: Int
        get() = R.layout.fragment_board

    override fun initView(view: View?, savedInstanceState: Bundle?) {
        sendMesPresenter = PublicPresenter(context, false, "")
        sendMesPresenter?.attachView(this)
        refresh_layout = view?.findViewById(R.id.refresh_layout)
        recycler_view = view?.findViewById(R.id.recycler_view)

        setBaseAdapter()
        Supplier()
        recycler_view?.useDefaultLoadMore()
        recycler_view?.loadMoreFinish(false, true)

        refresh_layout?.setOnRefreshListener {
            page = 1
            Supplier()
        }
        recycler_view?.setLoadMoreListener {
            page++
            Supplier()
        }

    }

    fun Supplier() {
        sendMesPresenter?.netWorkRequestPost(
            HttpURL.Req_getSupplierList, HttpRequest.getSupplierList(
                "",
                page
            )
        )
    }

    fun setBaseAdapter() {

        baseRecyclerAdapter = object : BaseRecyclerAdapter<BoardFragmentBean.DataBean.ListBean>(
            context,
            null,
            R.layout.board_fragment
        ) {
            override fun convert(
                holder: BaseRecyclerHolder?,
                data: BoardFragmentBean.DataBean.ListBean,
                position: Int,
                isScrolling: Boolean,
                selectedPosition: Int
            ) {
                setgoosAdapter()
                val layoutManager = LinearLayoutManager(context)
                layoutManager.orientation = LinearLayoutManager.HORIZONTAL
                val erisne_nse = holder?.getView<RecyclerView>(R.id.erisne_nse)
                erisne_nse?.layoutManager = layoutManager
                goosAdapter?.setData(data.goods)
                erisne_nse?.adapter = goosAdapter
                val iv_ismeas = holder?.getView<RoundImageView>(R.id.iv_ismeas)
                val name_tv = holder?.getView<TextView>(R.id.name_tv)
                val prov_tv = holder?.getView<TextView>(R.id.prov_tv)
                val description_tv = holder?.getView<TextView>(R.id.description_tv)
                val tel_tv = holder?.getView<TextView>(R.id.tel_tv)
                val mysause =holder?.getView<LinearLayout>(R.id.mysause)

                prov_tv?.text = "地区：" + data?.prov + data.city
                description_tv?.text = "供应：" + data.description
                tel_tv?.text = "电话：" + data.tel
                GlideUtil.load(context, data.logo, iv_ismeas)
                name_tv?.text = data.name
                mysause?.setOnClickListener {
                    val intent =Intent(context, SupplierActivity().javaClass)
                    intent.putExtra("id",data.id.toString())
                    intent.putExtra("name", data.name)
                    startActivity(intent)
                }
            }
        }
        recycler_view?.adapter = baseRecyclerAdapter
    }


    fun setgoosAdapter() {
        goosAdapter = object : BaseRecyclerAdapter<BoardFragmentBean.DataBean.ListBean.GoodsBean>(
            context,
            null,
            R.layout.baser_ecycler
        ) {
            override fun convert(
                holder: BaseRecyclerHolder?,
                data: BoardFragmentBean.DataBean.ListBean.GoodsBean,
                position: Int,
                isScrolling: Boolean,
                selectedPosition: Int
            ) {
                val iv_tuem = holder?.getView<ImageView>(R.id.iv_image)
                val tv_name =  holder?.getView<TextView>(R.id.tv_name)
                val discount_info =  holder?.getView<TextView>(R.id.discount_info)
                val mLlGoods = holder?.getView<LinearLayout>(R.id.mLlGoods)
                tv_name?.text =data.title
                discount_info?.text = "¥:"+data.marketprice
                GlideUtil.load(context, data.thumb, iv_tuem)

                mLlGoods?.setOnClickListener {
                    val Init = Intent(context, DetailsActivity().javaClass)
                    Init.putExtra("msid", data.id.toString())
                    Init.putExtra("Activity","GatherFragment")
                    context?.startActivity(Init)
                }
            }
        }
    }

    override fun onDestroyView() {
        sendMesPresenter?.detachView()
        super.onDestroyView()
    }

    override fun onSuccess(data: String?) {
        refresh_layout?.isRefreshing = false
        boardFragmentBean = GsonUtils.getGson().fromJson(data, BoardFragmentBean().javaClass)
        if (page == 1) {
            baseRecyclerAdapter?.clear()
        }
        if (boardFragmentBean?.data?.list?.size!! > 0) {
            recycler_view?.loadMoreFinish(false, true)
            baseRecyclerAdapter?.setData(boardFragmentBean?.data?.list)
        } else {
            recycler_view?.loadMoreFinish(true, false)
        }

    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }


}